package com.seaboxdata.jt809.common.util;

import com.mysql.cj.util.StringUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: Xiuming Lee
 * @Date: 2020/7/8 11:10
 * @Version 1.0
 * @Describe: 编码公共方法
 */
public class PropertiesUtil {

    private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
    private static Properties props;

    static {
        // 手动设置启动环境后,设置对应的配置文件
        String active = System.getProperty("--spring.profiles.active", "");
        log.info("active ={}", active);

        String fileName = "application" + (StringUtils.isNullOrEmpty(active) ? "" : "-" + active) + ".properties";
        props = new Properties();
        try {
            props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),
                    "UTF-8"));
        } catch (IOException e) {
            log.error("load configuration file failure!", e);
        }
    }

    public static String getProperty(String key) {
        String value = props.getProperty(key.trim());
        if (value.isEmpty()) {
            return null;
        }
        return value.trim();
    }

    public static String getProperty(String key, String defaultValue) {
        String value = props.getProperty(key.trim());
        if (value.isEmpty()) {
            value = defaultValue;
        }
        return value.trim();
    }
}
